import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import {
  getAdminList,
  getAdminById,
  createAdmin,
  updateAdmin,
  deleteAdmin,
  toggleAdminStatus,
  resetAdminPassword,
  assignRoles,
} from '@/api/admin'

const initialState = {
  adminList: [],
  currentAdmin: null,
  total: 0,
  loading: false,
  error: null,
}

// 获取管理员列表
export const fetchAdminList = createAsyncThunk(
  'admin/fetchAdminList',
  async (params, { rejectWithValue }) => {
    try {
      const data = await getAdminList(params)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 获取管理员详情
export const fetchAdminById = createAsyncThunk(
  'admin/fetchAdminById',
  async (id, { rejectWithValue }) => {
    try {
      const data = await getAdminById(id)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 创建管理员
export const addAdmin = createAsyncThunk(
  'admin/addAdmin',
  async (data, { rejectWithValue }) => {
    try {
      const result = await createAdmin(data)
      return result
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 更新管理员
export const modifyAdmin = createAsyncThunk(
  'admin/modifyAdmin',
  async ({ id, data }, { rejectWithValue }) => {
    try {
      const result = await updateAdmin(id, data)
      return result
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 删除管理员
export const removeAdmin = createAsyncThunk(
  'admin/removeAdmin',
  async (id, { rejectWithValue }) => {
    try {
      const data = await deleteAdmin(id)
      return { id, data }
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 切换管理员状态
export const changeAdminStatus = createAsyncThunk(
  'admin/changeAdminStatus',
  async ({ id, status }, { rejectWithValue }) => {
    try {
      const data = await toggleAdminStatus(id, status)
      return { id, status, data }
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 重置密码
export const resetPassword = createAsyncThunk(
  'admin/resetPassword',
  async ({ id, newPassword }, { rejectWithValue }) => {
    try {
      const data = await resetAdminPassword(id, newPassword)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 分配角色
export const assignAdminRoles = createAsyncThunk(
  'admin/assignAdminRoles',
  async ({ id, roleIds }, { rejectWithValue }) => {
    try {
      const data = await assignRoles(id, roleIds)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

const adminSlice = createSlice({
  name: 'admin',
  initialState,
  reducers: {
    clearError: (state) => {
      state.error = null
    },
    clearCurrentAdmin: (state) => {
      state.currentAdmin = null
    },
  },
  extraReducers: (builder) => {
    builder
      // 获取管理员列表
      .addCase(fetchAdminList.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchAdminList.fulfilled, (state, action) => {
        state.loading = false
        state.adminList = action.payload.data || action.payload.list || []
        state.total = action.payload.total || 0
      })
      .addCase(fetchAdminList.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 获取管理员详情
      .addCase(fetchAdminById.pending, (state) => {
        state.loading = true
      })
      .addCase(fetchAdminById.fulfilled, (state, action) => {
        state.loading = false
        state.currentAdmin = action.payload.data || action.payload
      })
      .addCase(fetchAdminById.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 创建管理员
      .addCase(addAdmin.pending, (state) => {
        state.loading = true
      })
      .addCase(addAdmin.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(addAdmin.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 更新管理员
      .addCase(modifyAdmin.pending, (state) => {
        state.loading = true
      })
      .addCase(modifyAdmin.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(modifyAdmin.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 删除管理员
      .addCase(removeAdmin.pending, (state) => {
        state.loading = true
      })
      .addCase(removeAdmin.fulfilled, (state, action) => {
        state.loading = false
        state.adminList = state.adminList.filter((item) => item.id !== action.payload.id)
      })
      .addCase(removeAdmin.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 切换状态
      .addCase(changeAdminStatus.fulfilled, (state, action) => {
        const admin = state.adminList.find((item) => item.id === action.payload.id)
        if (admin) {
          admin.status = action.payload.status
        }
      })
  },
})

export const { clearError, clearCurrentAdmin } = adminSlice.actions
export default adminSlice.reducer

